package drest.service.swivel;

import java.util.Vector;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import org.apache.commons.httpclient.Credentials;

import org.apache.commons.httpclient.HttpState;

import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import drest.service.HttpRestService;
import be.roam.util.StringUtil;




public class SwivelService extends HttpRestService {

	//private static final String TWITTER_URL = "http://twitter.com/statuses/";
	private static final String SWIVEL_URL = "http://www.stage/rest";
	
	/**
     * The Swivel authentication scope.
     */
    //private static final AuthScope TWITTER_AUTH_SCOPE = new AuthScope("Twitter.com", 80, "Twitter API");
    private static final AuthScope SWIVEL_AUTH_SCOPE = new AuthScope("Swivel.com", 80, "Swivel API");
    
    /*
     * Swivel API key.
     */
    
    //private static final String API_KEY = "f1d8c6dfd72bbd223c5c1589aabdcaf5b3df3fd0";
    //private static final String USER = "thor@swivel.com";
    //private static final String PASS = "passw0rd";

    private static final String API_KEY = "";
    private static final String USER = "";
    private static final String PASS = "";
    
    
    /**
     * User name (email address).
     */
    private String login;

    /**
     * Password.
     */
    private String password;

    /**
     * The credentials (created from user name and password).
     */
    private Credentials credentials;

    /**
     * Constructor.
     */
    /*
    public SwivelService() {
        super();
    }*/

    /**
     * Constructor.
     *
     * @param login the screen name (or email address)
     * @param password the password for the user
     */
    public SwivelService() {
        super();
        setCredentials(USER, PASS);
    }

    /**
     * Sets the credentials for the service.
     *
     * @param userName the user name (email address)
     * @param password the password for the user
     */
    public void setCredentials(String userName, String password) {
        this.login = userName;
        this.password = password;
        credentials = new UsernamePasswordCredentials(userName, password);
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
        if (!StringUtil.isNullOrEmpty(password)) {
            credentials = new UsernamePasswordCredentials(login, password);
        }
    }

    /**
     * @return the screen name or email address
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the screen name or email address
     */
    public void setLogin(String login) {
        this.login = login;
        if (!StringUtil.isNullOrEmpty(login)) {
            credentials = new UsernamePasswordCredentials(login, password);
        }
    }

   /**
    * 
    */
    
    public String upload(String file_data, Vector upload_data) throws IOException {
    	client.getState().setCredentials(SWIVEL_AUTH_SCOPE, credentials);
    	PostMethod method = createPostMethod(SWIVEL_URL + "/data_sets");
    	method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    	method.setRequestHeader("Authorization", "Basic :" + API_KEY);
    	method.setParameter("name", (String)upload_data.elementAt(0));
    	method.setParameter("data", file_data); 
    	method.setParameter("mode", (String)upload_data.elementAt(2));
    	method.setParameter("original_asset_name", (String)upload_data.elementAt(3));
    	method.setParameter("original_asset_path", (String)upload_data.elementAt(4));
    	method.setParameter("column_separator", (String)upload_data.elementAt(5));
    	method.setParameter("auto_estimate", "true");
    	method.setParameter("citation", (String)upload_data.elementAt(6));
    	method.setParameter("display_tags", "taggy");
    	client.executeMethod(method);
    	return parseStream(method.getResponseBodyAsStream());
    }
    
    /**
     * 
     * @param args
     * @return String
     */
    
    public String getData(String data_type, String id) throws IOException {
    	client.getState().setCredentials(SWIVEL_AUTH_SCOPE, credentials);
    	
    	System.out.println("creds: " + this.credentials);
    	
    	GetMethod method = null;
    	method = getMethod(data_type, id);
    	method.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    	client.executeMethod(method);
    	return parseStream(method.getResponseBodyAsStream());
    }
    
    public String parseStream(InputStream response) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(response));
    	String xml = "";
    	String cur_line = br.readLine();
    	while (cur_line != null) {
    		xml += cur_line + "\n";
    		cur_line = br.readLine();
    	}
    	return xml;
    }
	
    public GetMethod getMethod(String data_type, String id) {
    	GetMethod method = null;
		if (data_type.equalsIgnoreCase("users")) {
			if (id != "") {
				method = createGetMethod(SWIVEL_URL + "/users/" + id);
			} else {
				method = createGetMethod(SWIVEL_URL + "/users");
			}
		} else if (data_type.equalsIgnoreCase("data_sets")) {
			if (id != "") {
				method = createGetMethod(SWIVEL_URL + "/data_sets/" + id);
			} else {
				method = createGetMethod(SWIVEL_URL + "/data_sets");
			}
    	} else if (data_type.equalsIgnoreCase("graphs")) {
    		if (id != "") {
    			method = createGetMethod(SWIVEL_URL + "/graphs/" + id);
    		} else {
    			method = createGetMethod(SWIVEL_URL + "/graphs");
    		}
    	}
    	return method;
	}
}
